define("admin/page/login", [
    "jquery",
    "vue",
    "vue-resource",
    "toastr"
], function($, Vue, VueHttp, toastr){
    Vue.use(VueHttp);
    var vm = new Vue({
        el : '#login',

        data: {
            email: '',
            password: '',
            captcha: '',
            count: 0,
            token: ''
        },
        methods: {
            submit: function() {
                var self = this;
                if (! this.email || ! this.password) {
                    toastr.warning('邮箱或者密码为空');
                    return;
                }
                this.$http.post("/admin/auth/login", {
                    'email': this.email,
                    'password': this.password,
                    'captcha': this.captcha,
                    '_token': this.token
                })
                .then(function(response){
                    return response.json();
                })
                .then(function(json){
                    if (json.ret == 0) {
                        toastr.success(json.msg);
                        setTimeout(function(){
                            window.location.href = json.data.url;
                        },2000)
                    } else {
                        self.count = json.data.count;
                        self.token = json.data.token;
                        $('#captcha').attr('src',json.data.url);
                        toastr.warning(json.msg);
                    }
                })
                .catch(function() {
                    toastr.warning('接口请求失败');
                });
            },
             refresh: function () {
                 this.$http.get("/admin/auth/refresh")
                     .then(function(response){
                         return response.json();
                     })
                     .then(function(json) {
                         if (json.ret == 0) {
                             //toastr.success(json.msg);
                             setTimeout(function(){
                                 $('#captcha').attr('src',json.data.url);
                             })
                         } else {
                             toastr.warning(json.msg);
                         }
                     })
                     .catch(function() {
                         toastr.warning('刷新验证码失败');
                     });
             }
        },

        mounted: function(){
            Vue.set(this, 'token', $("[name='csrf-token']").attr('content'));
            Vue.set(this, 'count', PAGE.meta.count);
        },
    });
});